package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import cn.com.kh.KHDBConfig
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * DABC对接APP
 *
 */

public class apiDynAppLogin implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiDynAppLogin.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);
        String dataSourceID = "dataSource";

        //获取所有参数名
        Enumeration e = request.getParameterNames();
        while (e.hasMoreElements()) {
            //获取当前参数名
            String parameterName = (String) e.nextElement();
            log.debug(parameterName + ": " + request.getParameter(parameterName));
        }

        // 获取嵌入传递的参数
        String opsource = request.getParameter("opsource");
        String username = request.getParameter("loginname");
        String expand = request.getParameter("expand");

        log.debug("回调参数信息【opsource】：" + opsource + "【userinfo】：" + username + "【expand】：" + expand);

        String isNullMSG = KHUtil.isNullMSG("opsource", opsource) + KHUtil.isNullMSG("username", username);
        if (!KHUtil.isNull(isNullMSG)) {
            return isNullMSG;
        }

        if (opsource == "app") {
            try {
                // 预定义登录账号信息
                String staffID = "";
                String staffName = "";
                String Rolegroup_ID = "";
                String Loginname = "";
                String Password = "";

             //   JSONObject userJson = new JSONObject(userinfo);
              //  String userId = KHUtil.GetJV(userJson, "userId");
              //  String mobile = KHUtil.GetJV(userJson, "mobile");

                String getStaffInfo = '''Select R1.Staff_Id,
                                               R1.Staffname,
                                               Case
                                                 When R1.Defaultrolegroup Is Null Or R1.Defaultrolegroup = '' Then
                                                  R2.Rolegroup_Id
                                                 Else
                                                  R1.Defaultrolegroup
                                               End As Rolegroup_ID,
                                               R1.agent_id,
                                               R1.Password
                                          From Staff R1
                                          Left Join Staffrolegroup R2
                                          On R1.Staff_Id = R2.Staff_Id
                                         Where R1.Workstatus = 'ONLINE\'
                                           And R1.loginname = \'''' + username + '''\''''

                List checkStaffRes = eliteBean.chaxun_sql(getStaffInfo, "dataSource");
                if (checkStaffRes.size() > 0) {
                    Map checkStaffMap = checkStaffRes.get(0);
                    staffID = checkStaffMap.get("Staff_Id").toString();
                    staffName = checkStaffMap.get("Staffname").toString();
                    Rolegroup_ID = checkStaffMap.get("Rolegroup_ID").toString();
                    Loginname = checkStaffMap.get("agent_id").toString();
                    Password = KHUtil.objIsNull(checkStaffMap.get("Password")) ? null : checkStaffMap.get("Password").toString();
                    log.debug("获取当前登录人在系统中的信息:" + staffID + "/" + staffName + "/" + Password);

                    // NGS Domain信息
                    String ngsDomain = KHDBConfig.getAPIURL(eliteBean, "APP-NGSDOM");

                    // 获取NGS页面信息短码
                    String NGSShortenLinkURL = ngsDomain + "?dynid=DHBII3&userid=" + staffID + "&usertype=staff&woid=&taskid=&expand=" + expand + "&cbtype=&opsource=" + opsource + "&user=" + Loginname + "&grp=" + Rolegroup_ID;
                    if (!KHUtil.objIsNull(Password)) {
                        NGSShortenLinkURL = NGSShortenLinkURL + "&pwd=" + URLEncoder.encode(Password, "UTF-8");
                    }
                    log.debug("获取NGS页面短码URL：" + NGSShortenLinkURL);

                    JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(NGSShortenLinkURL, "UTF-8", 5000, 0));
                    log.debug("获取NGS页面信息短码：" + apiLinkJson.toString());

                    // 解析NGS短码调用返回
                    String linkURL = apiLinkJson.getString("message");

                    response.sendRedirect(linkURL);

                } else {
                    log.warn("当前手机号码在系统不存在,打开工单异常...");
                    return "当前手机号码在系统不存在,打开工单异常...";

                }

            } catch (Exception e1) {
                log.error(e1);
                return "登录信息异常";
            }


        } else {
            log.warn("登录源系统标识无法识别,打开工单异常...");
            return "登录源系统标识无法识别,打开工单异常...";

        }
    }

}